<?php
namespace Tbf\Bundle\SequenceBundle\Tests;

use Tbf\Bundle\TestBundle\TestSuite\TbfTestCase;

class SequenceTest extends TbfTestCase
{
    function test1(){
        $ret = $this->getThisModel()->getNextNum('test1');
        $this->assertEquals(1,$ret);
        $ret = $this->getThisModel()->getNextNum('test1');
        $this->assertEquals(2,$ret);
        $ret = $this->getThisModel()->getNextNum('test2');
        $this->assertEquals(1,$ret);
    }
    function testGetNextLetter(){
        $ret = $this->getThisModel()->getNextLetter('test1','A');
        $this->assertEquals('A',$ret);
        $ret = $this->getThisModel()->getNextLetter('test1','AA');
        $this->assertEquals('AB',$ret);
        $ret = $this->getThisModel()->getNextLetter('test2','AZ');
        $this->assertEquals('AZ',$ret);
    }
    function testNumToLetter(){
        foreach($this->getLetterNumTable() as $num=>$letter){
            $ret = $this->getThisModel()->numToLetter($num);
            $this->assertEquals($letter,$ret,sprintf('NumToLetter fail!! num:%d,letter:%s',$num,$letter));
        }
    }
    function testLetterToNum(){
        foreach($this->getLetterNumTable() as $num=>$letter){
            $ret = $this->getThisModel()->letterToNum($letter);
            $this->assertEquals($num,$ret,sprintf('LetterToNum fail!! num:%d,letter:%s',$num,$letter));
        }
    }
    function getLetterNumTable(){
        return array(
            1=>'A',
            25=>'Y',
            26=>'Z',
            27=>'AA',
            52=>'AZ',
            53=>'BA',
            26*26-1=>'YY',
            26*26=>'YZ',
            26*26+1=>'ZA',
            26*(26+1)-1=>'ZY',
            26*(26+1)=>'ZZ',
            26*(26+1)+1=>'AAA',
            26*(26*(26+1)+1)+1=>'AAAA',
            534=>'TN',
        );
    }
    /**
     * @return \Tbf\Bundle\SequenceBundle\SequenceManager
     */
    function getThisModel(){
        return $this->get('tbf_sequence');
    }
    function setUp(){
        if (!$this->getContainer()->has('tbf_sequence')){
            $this->markTestSkipped();
        }
        $this->setDbData(array(
            'tbf_sequence'=>array()
        ));
    }
}